package org.onionframework.persistence.dao.processor;

import org.hibernate.criterion.Criterion;
import org.onionframework.persistence.dao.CriteriaBuilder;
import org.onionframework.persistence.dao.PropertyFilterProcessor;
import org.onionframework.persistence.dao.expression.OnionExpressions;
import org.onionframework.persistence.query.DatePart;

public class DatePartProcessor implements PropertyFilterProcessor {

	private String op;
	
	public DatePartProcessor(String op){
		this.op = op;
	}
	
	@Override
	public Criterion buildCriterion(String alias, String name, Object value,
			CriteriaBuilder context) {
		Object[] values = (Object[])value;
		if("eq".equals(op)){
			return OnionExpressions.datePartEq(alias, (Integer)values[1], (DatePart)values[0]);
		}else if("le".equals(op)){
			return OnionExpressions.datePartLe(alias, (Integer)values[1], (DatePart)values[0]);
		}else if("lt".equals(op)){
			return OnionExpressions.datePartLt(alias, (Integer)values[1], (DatePart)values[0]);
		}else if("ge".equals(op)){
			return OnionExpressions.datePartGe(alias, (Integer)values[1], (DatePart)values[0]);
		}else if("gt".equals(op)){
			return OnionExpressions.datePartGt(alias, (Integer)values[1], (DatePart)values[0]);
		}else
			throw new RuntimeException("不可识别的操作符：" + op);
	}

}
